﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OpenAdmin.Application.Services;
using OpenAdmin.Application.Services.Dtos;

namespace OpenAdmin.HttpApi.Controllers;

/// <summary>
/// 用户相关Api
/// </summary>
[ApiExplorerSettings(GroupName = "Application")]
public class UserController : ApiControllerBase
{
    private readonly IUserService _userServices;
    /// <summary>
    /// 构造函数
    /// </summary>
    public UserController(IUserService userServices)
    {
        _userServices = userServices;
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(UserInfoResponseDto), 200)]
    public async Task<IActionResult> GetUserInfo()
    {
        return Ok(await _userServices.GetUserInfo());
    }

    /// <summary>
    /// 获取用户列表(分页)
    /// </summary>
    /// <returns></returns>
    [HttpGet, ProducesResponseType(typeof(PagedList<UserResponseDto>), 200)]
    public async Task<IActionResult> GetUsersByPage([FromQuery] UserRequestDto requestDto)
    {
        return Ok(await _userServices.GetUsersByPage(requestDto));
    }

    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="requestDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> AddUser(AddUserRequestDto requestDto)
    {
        await _userServices.AddUser(requestDto);

        return Ok();
    }

    /// <summary>
    /// 更新用户信息
    /// </summary>
    /// <param name="requestDto"></param>
    [HttpPost]
    public async Task<IActionResult> EditUser(EditUserRequestDto requestDto)
    {
        await _userServices.EditUser(requestDto);

        return Ok();
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="id"></param>
    [HttpPost]
    public async Task<IActionResult> DeleteUser(int id)
    {
        await _userServices.DeleteUser(id);

        return Ok();
    }

    /// <summary>
    /// 为用户分配角色
    /// </summary>
    /// <param name="requestDto"></param>
    [HttpPost]
    public async Task<IActionResult> DistributionRole(DistributionRoleRequestDto requestDto)
    {
        await _userServices.DistributionRole(requestDto);

        return Ok();
    }
}
